import type { PluginOption } from "vite";
import vue from "@vitejs/plugin-vue";
import vueLegacy from "@vitejs/plugin-legacy";
import createInspector from "./inspector";
import createAutoImport from "./auto-import";
import createComponents from "./components";
import createSvgIcon from "./svg-icon";
import createPrismjs from "./prismjs";
import createCompression from "./compression";
import createBanner from "./banner";
import createConsole from "./console";
import createUnocss from "./unocss";
import VueDevTools from 'vite-plugin-vue-devtools'
import { visualizer } from "rollup-plugin-visualizer";

export default function createVitePlugins(viteEnv: Record<string, any>) {
  const isBuild = viteEnv.VITE_COMMAND === "build"
  const vitePlugins: (PluginOption | PluginOption[])[] = [
      vue(),
      createPrismjs(),
      createInspector(),
      createAutoImport(),
      createComponents(),
      createConsole(),
      createUnocss(),
      createBanner(),
      VueDevTools(),
      createSvgIcon(isBuild),
      vueLegacy({
          renderLegacyChunks: false,
          modernPolyfills: ["es.global-this","es.array.at", "es.array.find-last"],
      }),
  ];

  isBuild && vitePlugins.push(...createCompression(viteEnv));

  if (isBuild) {
    vitePlugins.push(
      visualizer({
        gzipSize: true,
        brotliSize: true,
        open: true,
        filename: "public/report.html",
        title: "Bundle Analyzer",
      }),
    );
  }
  return vitePlugins;
}
